package com.auto.survey.activity.config

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class SwipeMenuCallback(private val adapter: SurveyPointAdapter) : ItemTouchHelper.Callback() {

    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
        val swipeFlags = 0 // 禁用滑动删除
        return makeMovementFlags(dragFlags, swipeFlags)
    }

    override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
        if (viewHolder.adapterPosition != target.adapterPosition) {
            adapter.moveItem(viewHolder.adapterPosition, target.adapterPosition)
            return true
        }
        return false
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        // 不自动删除，由用户点击删除按钮触发
    }

    // 拖拽开始时的回调
    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        super.onSelectedChanged(viewHolder, actionState)
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            // 高亮显示正在拖拽的项目
            viewHolder?.itemView?.alpha = 0.8f
        }
    }

    // 拖拽结束时的回调
    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        // 恢复项目的视觉状态
        viewHolder.itemView.alpha = 1.0f
    }

    // 控制是否可以长按开始拖拽
    override fun isLongPressDragEnabled(): Boolean = false // 禁用长按拖拽

    // 控制是否可以滑动
    override fun isItemViewSwipeEnabled(): Boolean = false // 禁用滑动
}